package org.lex.filter;

import java.util.ArrayList;

public class Filters {
	@SuppressWarnings("unchecked")
	public static <T> T[] filter(T[] values, Filter<T> filter) {
		if (null == values || 0 == values.length)
			return values;
		ArrayList<T> list = new ArrayList<T>();
		for (T e : values)
			if (filter.accept(e))
				list.add(e);
		return (T[]) list.toArray();
	}

	@SuppressWarnings("unchecked")
	public static <T> Filter<T> acceptAllFilter() {
		return ConstantsFilter.ACCEPT_ALL;
	}

	@SuppressWarnings("unchecked")
	public static <T> Filter<T> rejectAllFilter() {
		return ConstantsFilter.REJECT_ALL;
	}

	public static <T> Filter<T> not(Filter<T> filter) {
		return new NotFilter<T>(filter);
	}

	public static <T> MultipleFilter<T> and(Filter<T>... filters) {
		return new AndFilter<T>(filters);
	}

	public static <T> MultipleFilter<T> or(Filter<T>... filters) {
		return new OrFilter<T>(filters);
	}

	private Filters() {
	}
}
